package com.rasenhund.android.grecorder.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.common.Const;
import com.rasenhund.android.grecorder.dao.RecordDao;
import com.rasenhund.android.grecorder.helper.DbHelper;

/**
 * 勝敗選択画面
 */
public class SelectResultActivity extends ConfirmBaseActivity {
    @SuppressWarnings("unused")
    private static final String TAG = SelectResultActivity.class.getSimpleName();
    private final SelectResultActivity self = this;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_result);
        
        // Viewの初期化
        initView();
        
    }
    
    final OnClickListener btnWin_OnClickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            
            setResultCd(Const.RESULT.WIN);
            registRecord();
            jumpMenu();
            
        }
    };
    
    final OnClickListener btnLose_OnClickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            
            setResultCd(Const.RESULT.LOSE);
            registRecord();
            jumpMenu();
            
        }
    };
    
    final OnClickListener btnDraw_OnClickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            
            setResultCd(Const.RESULT.DRAW);
            registRecord();
            jumpMenu();
            
        }
        
    };
    
    /**
     * Viewの初期化
     */
    private void initView(){

        Button btnWin = (Button)findViewById(R.id.btnWin);
        btnWin.setOnClickListener(btnWin_OnClickListener);

        Button btnLose = (Button)findViewById(R.id.btnLose);
        btnLose.setOnClickListener(btnLose_OnClickListener);
        
        Button btnDraw = (Button)findViewById(R.id.btnDraw);
        btnDraw.setOnClickListener(btnDraw_OnClickListener);
        
    }

    /**
     * パラメータ用変数に結果コードをセット
     * @param resultCd
     */
    private void setResultCd(int resultCd){
        
        mRecord.setResultCd(resultCd);
        
    }
    
    /**
     * 戦歴の登録処理
     */
    private void registRecord(){

        DbHelper dbHelper = new DbHelper(self);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        RecordDao dao = new RecordDao(db);
        
        long iRet = dao.insert(mRecord);
        
        db.close();
        
        if (iRet != 0) {
            String sMsg = getResources().getString(R.string.msg_regist_complete);
            Toast.makeText(self, sMsg, Toast.LENGTH_LONG).show();
        }
    }
    
    /**
     * メニューへ戻る
     * @param category
     */
    private void jumpMenu(){
        
        Intent intent = new Intent(self, MenuActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        
    }
    
}
